 
import  AopFactory  from  '../AopFactory.js';
import  Pack from '../utils/Pack.js'
import  ConfigLoad  from  './ConfigLoad.js';
import  LibDirLoad from './LibDirLoad.js'


//let JSZip=ZStream;
/**
 * 扫描路径
 * @param path
 * @param name
 * @returns {*}
 */
export default class JsrLoad{

    constructor(){
        this. configs=[]
        this.libDirs=[];
        this.runDirs=[];
    }
    load(moduleName){
        let _this=this;
        this.configs=new ConfigLoad().load(moduleName);
         this.libDirs=new LibDirLoad().load(moduleName);

        let dirs=new Dirs();
        for(let i=0; i<this.configs.length;i++){
          let disss=  dirs.isDir(this.configs[i],this.libDirs)
            this.runDirs.push(...disss)
        }
        let dir= new Dir("reactTest","com/setcc/aop");
        dir.dirs.push("com/setcc/service")
        this.runDirs.push(dir );

           _this. _load( )


    }

    _load( ){
        let _this= this;
        let aopFactory=new AopFactory();
        for (let i=0;i<_this.runDirs.length;i++){
            let jsr= qguan.root.findData(_this.runDirs[i].module,"","","jsrLoad");
              jsr.then(function (data) {
                  let pack = new Pack();
                  pack.setData(data)
                  let paths=pack.findPath();
                  let pathslist=paths.split("/n");
                  let dirNames=  _this.runDirs[i].dirs;
                  for(let l=0;l<dirNames.length;l++) {
                      let reg = new RegExp(dirNames[l])
                      debugger
                      for(let n=0;n<pathslist.length;n++){
                          let dirs= pathslist[n].split(",")
                          let dir=dirs[0].replace(new RegExp(/\\/g), '/')
                          if( reg.test(dir)&& dirs[3]=="file"){
                                  let code=pack.findText(dirs[1],dirs[2]).replace(new RegExp(/\00/g),"")
                                  let filename = dir.substring(dir.lastIndexOf('\/') + 1, dir.lastIndexOf('.'));
                                  let obj =aopFactory.createCodeObj(_this.runDirs[i].module,filename,code);
                          }

                      }

                  }
              })

        }
    }
}

/**
 * 通过config  需要加载的路径
 *
 */
class Dirs{
    constructor(){
        this. dirs=[]
        this.dirNames={}
    }
    isDir(config,libDirs){
        this.dirs=[]
        this.dirNames={}
        for(let l=0;l<libDirs.length;l++){
            if(libDirs[l][1].indexOf(config) != -1 ){
                let j=dirNames[libDirs[l][0]]
                if(j){
                    this. dirs.push(new Dir(libDirs[l][0],libDirs[l][1]))
                }else {
                    this. dirs[j].dirs.push(dir)
                }
            }
        }
        return this.dirs;
    }

}

class Dir{
    constructor(module,dir) {
        this.module = module
        this.dirs=[dir]
    }

}


